<?php
namespace jy_weishop\page;
defined('BY_JYA') or exit('error');
/*
	页面返回
*/
class member_index extends \plugin{
	public function getmain($return=array()){
		global $_RQ;
		$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
		$return['show_setmobile']=cfc('core')->check_setmobile(1);
		$return['show_setmember']=cfc('core')->check_setmember(1);
		//升级会员：
		//$this->model('member')->check_uplevel($_SESSION['uid']);
		//注册电商客会员
		$item=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit5','credit6','mobile','avatar','nickname','username'));
		$item['id']=$_SESSION['uid'];
		$item['nickname']=$item['nickname']?:$item['username'];
		$item['nickname']=cfc('tools')->substr($item['nickname'],0,12);
		$member=$this->pdo_get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>0),array('level','viptime'));
		$member['level_id']=$member['level'];
		$member['ideal_money']=pdo_getcolumn('core_ideal_money',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
		$item['ideal_money']=$member['ideal_money']?:'0.00';
		$item['mycoupon']=pdo_count('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'islock'=>0,'isuse'=>0));
		if($return['setting']['no_vipshow']!=1){
			$return['nextlevel']=$this->model('member')->get_nextlevel(0,true);
			$item['cardbg']=$return['nextlevel']['nowcardbg'];
			$item['level']=$return['nextlevel']['nowtitle'];
		}else{
			$levelid=$member['level'];
			$mylevel=pdo_get('jy_weishop_member_level',array('id'=>$levelid,'uniacid'=>$_SESSION['uniacid']));
			$item['cardbg']=$mylevel['cardbg'];
			$item['level']=$mylevel['title'];
		}
		$item['viptime']=$member['viptime']?date('Y-m-d',$member['viptime']):'';
		$return['nextlevel']['cardbg']=tomedia($return['nextlevel']['cardbg']);
		$return['nextlevel']['vipimgs']=tomedia($return['nextlevel']['vipimgs']);
		$item['level_id']=$member['level'];
		$item['level']=$item['level']?$item['level']:'普通会员';
		$prizeType=array();
		$credit_types=credit_types();
		$pay=uni_setting('pay');
		foreach($credit_types as $k=>$cv){
			if($k>2){
				$prizeType[]=array(
					'id'=>$k,
					'title'=>$cv,
					'api'=>$pay['credit'.$k.'_api'],
					'data'=>$item['credit'.$k],
				);
			}
		}
		$item['credit_type']=$prizeType;
		if($_SESSION['os']=='windows' && $return['pc_set']['no_pc']!=1){
			$return['setting']=$this->plugin_setting('basic');
			$return['member']=$item;
			exi($return);
		}else{
			$return['setting']=$return['setting']?:$this->plugin_setting('setting');
			foreach($return['setting']['footernav'] as &$fv){
				$fv['url']=$fv['action'];
			}
		}
		$my_days = array(
					array('title'=>'我的优惠券','icon'=>'icon-youhuiquan','color'=>'#cc99ff','action'=>'jy_weishop/coupon.myindex/'),
					array('title'=>'微信会员卡','icon'=>'icon-daifahuo','color'=>'#cc99ff','action'=>'core/member.wxcard/main/'),
					array('title'=>'余额提现','icon'=>'icon-dixiaofei','color'=>'#cc99ff','action'=>'core/member.tixian/'),
					array('title'=>'地址管理','icon'=>'icon-position','color'=>'#cc99ff','action'=>'core/member.address/'),
					array('title'=>'帮助中心','icon'=>'icon-help','color'=>'#cc99ff','action'=>'jy_weishop/news.list/'),
					array('title'=>'获得购物卡','icon'=>'icon-qiangdan','color'=>'#cc99ff','action'=>'jy_weishop/member.getcard/'),
					array('title'=>'我的购物车','icon'=>'icon-gouwuche','color'=>'#cc99ff','action'=>'jy_weishop/member.cart/'),
					array('title'=>'我的购物卡','icon'=>'icon-pingzhengzhongxin','color'=>'#cc99ff','action'=>'jy_weishop/member.mycard/'),
					array('title'=>'我的关注','icon'=>'icon-woguanzhudepinpai','color'=>'#cc99ff','action'=>'jy_weishop/member.favorite/'),
					array('title'=>'我的足迹','icon'=>'icon-zuji','color'=>'#cc99ff','action'=>'jy_weishop/member.history/'),
					array('title'=>'门店列表','icon'=>'icon-lianxishangjia','color'=>'#cc99ff','action'=>'jy_weishop/store.index/'),
		);
		$return['setting']['my_days'] = !empty($return['setting']['my_days']) ? $return['setting']['my_days'] : $my_days;
		$my_logs = array(
					array('title'=>'积分日志','icon'=>'icon-jifen','color'=>'#cc99ff','action'=>'core/member.credit_log/list/type=1'),
					array('title'=>'余额日志','icon'=>'icon-jiangjiatongzhi','color'=>'#cc99ff','action'=>'core/member.credit_log/list/type=2'),
					array('title'=>'提现日志','icon'=>'icon-trade','color'=>'#cc99ff','action'=>'core/member.cash_log/'),
					array('title'=>'红包日志','icon'=>'icon-ai-hongbao','color'=>'#cc99ff','action'=>'core/member.red_log/'),
		);
		$return['setting']['my_logs'] = !empty($return['setting']['my_logs']) ? $return['setting']['my_logs'] : $my_logs;
		$my_memberbtn = array(
					array('title'=>'小程序直播','icon'=>'icon-video','color'=>'#cc99ff','action'=>'jy_weishop/sale.live_index/'),
					array('title'=>'领取优惠券','icon'=>'icon-youhuiquan1','color'=>'#cc99ff','action'=>'jy_weishop/coupon.index/'),
					array('title'=>'领取微信会员卡','icon'=>'icon-huiyuanqia','color'=>'#cc99ff','action'=>'core/member.wxcard/main/'),
					array('title'=>'积分排行','icon'=>'icon-paixing','color'=>'#cc99ff','action'=>'jy_weishop/member.rank/'),
		);
		if($return['shopset']['fenxiao_app'] && $_SESSION['current_plugin'] !='jy_friends_shop'){
			$fenxiaoset=plugin($return['shopset']['fenxiao_app'])->plugin_setting('page_setting');
			$action=$return['shopset']['fenxiao_app'].'/index/';
			if($return['shopset']['fenxiao_app']=='jy_newshopfee'){
				$action='jy_newshopfee/agent.index/';
			}
			$my_memberbtn[]=array('action'=>$action,'title'=>$fenxiaoset['tcenter']?:'我的分销','icon'=>'icon-paihao0','remark'=>'');
		}
		$install = $this->plugin_exists('jy_merch');
		if($install && $_SESSION['current_plugin'] !='jy_friends_shop'){
			$my_memberbtn[]=array('action'=>'jy_merch/manage.index/','title'=>'商户入口','icon'=>'icon-dianpu','remark'=>'');
		}
		$install = ('jy_giftbao');
		if($install && $_SESSION['current_plugin'] !='jy_friends_shop'){
			$my_memberbtn[]=array('action'=>'jy_weishop/sale.mylibao/','title'=>'我的礼包','icon'=>'icon-gifts','remark'=>'');
		}
		$install = $this->plugin_exists('jy_area_agent');
		if($install){
			$area_set=plugin('jy_area_agent')->plugin_setting('setting');
			if($area_set['member_show']){
				$jy_area_agenttitle=plugin('jy_area_agent')->plugin_setting('page')['agent_title']?:'区域代理';
				$my_memberbtn[]=array('action'=>'jy_area_agent/index/','title'=>$jy_area_agenttitle,'icon'=>'icon-paihao0','remark'=>'');
			}
		}
		$install = $this->plugin_exists('jy_globonus');
		if($install && $_SESSION['current_plugin'] !='jy_friends_shop'){
			$jy_globonus=plugin('jy_globonus')->plugin_setting('setting');
			if($jy_globonus['home']){
				$my_memberbtn[]=array('action'=>'jy_globonus/index/','title'=>'股东中心','icon'=>'icon-paihao0','remark'=>'');
			}
		}
		$install = $this->plugin_exists('jy_area_dian');
		if($install && $_SESSION['current_plugin'] !='jy_friends_shop'){
			$jy_area_dian=plugin('jy_area_dian')->plugin_setting('setting');
			if($jy_area_dian['member_show']){
				$my_memberbtn[]=array('action'=>'jy_area_dian/index/','title'=>'自提商入口','icon'=>'icon-paihao0','remark'=>'');
			}
		}
		if($this->plugin_exists('jy_commission') && $return['shopset']['fenxiao_level']=='jy_commission'){
			$level_id=pdo_getcolumn('jy_commission_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level_id');
			if($level_id){
				$item['level']=pdo_getcolumn('jy_commission_level',array('id'=>$level_id),'title');
			}else{
				$item['level']='普通等级';
			}
		}elseif($this->plugin_exists('jy_teamfee') && $return['shopset']['fenxiao_level']=='jy_teamfee'){
			$level_id=pdo_getcolumn('jy_teamfee_team',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level_id');
			if($level_id){
				$item['level']=pdo_getcolumn('jy_teamfee_level',array('id'=>$level_id),'title');
			}else{
				$item['level']='预选团长';
			}
		}elseif($this->plugin_exists('jy_levelfee') && $return['shopset']['fenxiao_level']=='jy_levelfee'){
			$level_id=pdo_getcolumn('jy_levelfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level_id');
			if($level_id){
				$item['level']=pdo_getcolumn('jy_levelfee_level',array('id'=>$level_id),'title');
			}else{
				$item['level']='普通等级';
			}
		}elseif($this->plugin_exists('jy_newshopfee') && $return['shopset']['fenxiao_level']=='jy_newshopfee'){
			$level_id=pdo_getcolumn('jy_newshopfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level_id');
			if($level_id){
				$item['level']=pdo_getcolumn('jy_newshopfee_level',array('id'=>$level_id),'title');
			}else{
				$item['level']='普通等级';
			}
		}
		$return['setting']['my_memberbtn'] = !empty($return['setting']['my_memberbtn']) ? $return['setting']['my_memberbtn'] : $my_memberbtn;
		$return['item']=$item;
		$status0 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'deleted'=>0,'userdeleted'=>0,'plugin_name <>'=>'jy_newcar'));
		$status1 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'deleted'=>0,'userdeleted'=>0,'plugin_name <>'=>'jy_newcar'));
		$status2 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2,'deleted'=>0,'userdeleted'=>0,'plugin_name <>'=>'jy_newcar'));
		$status3 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>3,'iscomment'=>0,'deleted'=>0,'userdeleted'=>0,'plugin_name <>'=>'jy_newcar'));
		$cartnums= pdo_getsum('jy_weishop_member_cart',array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
		$return['statics']=array(
			'status0'=>$status0,
			'status1'=>$status1,
			'status2'=>$status2,
			'status3'=>$status3,
			'cartnums'=>$cartnums
		);
		$wxcard_setting=uni_setting('wx_card_setting');
		$wxcard_edit=uni_setting('wx_card_edit');

		if($wxcard_edit['card_id'] && $wxcard_edit['status']==1 && $wxcard_setting['open']){
			//存在会员卡且允许领取微信会员卡，检查是否已领卡
			$check=pdo_getcolumn('core_fans',array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'wxgid'=>$_SESSION['wechat_id']),'wxcard_code');
			if(!$check){
				$wxcard=1;
			}
		}
		if(!$wxcard){
			array_splice($menu_list,3,1);
		}
		//判断配送员
		$install = check_account_permission('jy_peisong');
		if($install){
			$return['is_peisong'] = pdo_getcolumn('jy_peisong_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
		}
		//判断店员
		$return['is_worker'] = pdo_getcolumn('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
		if(!$return['is_worker']){
			$return['is_worker'] = pdo_getcolumn('jy_weishop_store_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
		}
		$return['tmplIds']=array();
		/*$return['htmls']['list'][]=array('type'=>'menu_list','list'=>$menu_list);
		$return['htmls']['list'][]=array('type'=>'submit_list','list'=>array(
			array('url'=>'core/member.cpassword/','title'=>'修改密码'),
			array('url'=>'core/member.logout/','title'=>'退出登陆','title_color'=>'#fff','background'=>'red'),
		));*/
		$sale_luck=plugin('jy_weishop')->plugin_setting('sale_luck');
		if($sale_luck['isopen']==1){
			$return['sale_luck']=model('jy_weishop/activity/sales')->getluck(true);
		}
		$return['closedsales']=1;
		$return['setpay']=uni_setting('pay');
		$return['menu_list']=$menu_list;
		$return['feemoney']=$this->plugin_setting('feemoney');
		if($return['feemoney']['isopen']==1){
			//节省数据
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$monthtime=mktime(0,0,0,date('m'),1,date('Y'));
			$yearstime=strtotime(date("Y",time())."-1"."-1");
			$lastyearstime=strtotime(date("Y",time())."-2"."-1");
			$daysgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$daytime));
			$return['feemoney_day'] = 0;
			foreach($daysgoods as $dv){
						$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
						if($status!=1 && $status!=2 && $status!=3){
							continue;
						}
						$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
						$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
						if($dv['feemoney']>0){
							$dv['feemoney']=$dv['total']*$dv['feemoney'];
							$return['feemoney_day'] +=$dv['feemoney'];
						}
			}
			$return['feemoney_day']=price($return['feemoney_day']);
			
			$monthgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$monthtime));
			$return['feemoney_month'] = 0;
			foreach($monthgoods as $dv){
						$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
						if($status!=1 && $status!=2 && $status!=3){
							continue;
						}
						$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
						$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
						if($dv['feemoney']>0){
							$dv['feemoney']=$dv['total']*$dv['feemoney'];
							$return['feemoney_month'] +=$dv['feemoney'];
						}
			}
			$return['feemoney_month']=price($return['feemoney_month']);
			
			$yearsgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$yearstime));
			$return['feemoney_years'] = 0;
			foreach($yearsgoods as $dv){
						$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
						if($status!=1 && $status!=2 && $status!=3){
							continue;
						}
						$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
						$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
						if($dv['feemoney']>0){
							$dv['feemoney']=$dv['total']*$dv['feemoney'];
							$return['feemoney_years'] +=$dv['feemoney'];
						}
			}
			$return['feemoney_years']=price($return['feemoney_years']);
			
			$lastyearsgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime between'=>array($yearstime,$lastyearstime)));
			$return['feemoney_lastyears'] = 0;
			foreach($lastyearsgoods as $dv){
						$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
						if($status!=1 && $status!=2 && $status!=3){
							continue;
						}
						$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
						$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
						$dv['feemoney']=$dv['total']*$dv['feemoney'];
						$return['feemoney_lastyears'] +=$dv['feemoney'];
			}
			$return['feemoney_lastyears']=price($return['feemoney_lastyears']);
		}
		$return['sale_tan']=model('jy_weishop/activity/sales')->gettan(4);
		$current_plugin=$_SESSION['current_plugin']?:'jy_weishop';
		if($return['sale_luck']){
				$sharelink=$current_plugin.'/index//fromuser='.$_SESSION['uid'].'&luckkey='.$return['sale_luck']['luckkey'];
				if($sale_luck['share_title']){
					$sale_luck['share_title']=str_replace('[lucktitle]',$return['sale_luck']['title'],$sale_luck['share_title']);
				}
				if($sale_luck['share_desc']){
					$sale_luck['share_desc']=str_replace('[lucktitle]',$return['sale_luck']['title'],$sale_luck['share_desc']);
				}
				$return['sharedata']=array(
					'title'=>$sale_luck['share_title']?$sale_luck['share_title']:'我今日运势《'.$return['sale_luck']['title'].'》，快来借势吧,好运势享大折扣！',
					'desc'=>$sale_luck['share_desc']?$sale_luck['share_desc']:'拥有好运势，不仅仅拥有好心情，更能享受好的购物折扣哦！',
					'link'=>custom_url($sharelink,true),
					'imgUrl'=>$sale_luck['share_image']?tomedia($sale_luck['share_image']):tomedia('../addons/jy_weishop/resource/images/sales/luck_share_image.jpg')
				);
		}else{
				$sharelink=$current_plugin.'/index//fromuser='.$_SESSION['uid'];
				$return['sharedata']=array(
					'title'=>$setting['share_title']?$setting['share_title']:'首页-在线商城',
					'desc'=>$setting['share_desc']?$setting['share_desc']:'在线商城，一个属于自己的个人商城！',
					'link'=>custom_url($sharelink,true),
					'imgUrl'=>ATTACHMENT_URL.$setting['share_image']
				);
		}
		$member_setting=uni_setting('member_setting');
		$return['sign_open']=$member_setting['sign']['open'];
		$cash=uni_setting('cash');
		$return['openjifencash']=$cash['openjifen'];
		$return['orderlist_url']='jy_weishop/order.list/';
		return $return;
	}
}